#include "Ladder.h"

Ladder::Ladder(void)
{
	numberOfSteps = 1;
	ladder = new Object[numberOfSteps];
	ladder[0].InitSprite(1);
	ladder[0].SetSprite(0, "images/Obstacle/ladder.tga");

	half = 0;

	minY = 0;
	maxY = 0;

	counter = 0;

	type = STATIC;
}

Ladder::~Ladder(void)
{
	if(ladder != NULL)
	{
		delete [] ladder;
		ladder = NULL;
	}
}

void Ladder::InitLadderHeight(Vector3D position, int numberOfSteps, LADDERTYPE type)
{
	ladder = new Object[numberOfSteps];
	this->numberOfSteps = numberOfSteps;
	this->type = type;

	if(type == MOVING)
	{
		this->half = numberOfSteps / 2;

		for(int i = 0; i < numberOfSteps; i++)
		{
			ladder[i].InitSprite(1);
			ladder[i].SetSprite(0, "images/Obstacle/ladder.tga");

			if(i < half)
			{
				ladder[i].Init(Vector3D(position.x - 2.5, position.y + i * 10, 0), 25, 10);
				//ladder[i].SetSpeed(0.1);
				ladder[i].SetDirection(Vector3D(0, -1, 0));
			}
			else
			{
				ladder[i].Init(Vector3D(position.x, position.y + i * 10, 0), 20, 10);
			}
		}

		this->minY = ladder[numberOfSteps - 1].GetPosition().y;
		this->maxY = ladder[0].GetPosition().y;

		ladder[numberOfSteps - 1].InitLine(Vector3D(ladder[0].GetPosition().x + ladder[0].GetWidth() / 2, ladder[0].GetPosition().y, ladder[0].GetPosition().z),
						Vector3D(ladder[numberOfSteps - 1].GetPosition().x + ladder[numberOfSteps - 1].GetWidth() / 2, ladder[numberOfSteps - 1].GetPosition().y + 10, ladder[numberOfSteps - 1].GetPosition().z));
	}
	else
	{
		for(int i = 0; i < numberOfSteps; i++){
			ladder[i].InitSprite(1);
			ladder[i].SetSprite(0, "images/Obstacle/ladder.tga");

			ladder[i].Init(Vector3D(position.x, position.y + i * 10, 0), 20, 10);
		}

		ladder[numberOfSteps - 1].InitLine(Vector3D(position.x + 20 / 2, position.y, position.z),
						Vector3D(position.x + 20 / 2, ladder[numberOfSteps - 1].GetPosition().y + 10, 0));
	}
}

void Ladder::DrawLadder(void)
{
	for(int i = 0; i < numberOfSteps; i++)
	{
		//ladder[i].SetPosition(Vector3D(ladder[i].GetPosition().x, ladder[i].GetPosition().y + i * 40, 0));
		ladder[i].RenderObject();
	}
}

void Ladder::LetLadderMove(float elapsed)
{
	if(type == MOVING)
	{
		if(ladder[0].GetPosition().y < maxY || ladder[half - 1].GetPosition().y > minY)
		{
			if(counter < 20)
			{
				counter += 1 * elapsed;
			}
			else
			{
				counter = 0;

				for(int i = 0; i < half; i++)
				{
					ladder[i].SetDirection(Vector3D(ladder[i].GetDirection().x, ladder[i].GetDirection().y * -1, ladder[i].GetDirection().z));
				}
			}
		}

		for(int i = 0; i < half; i++)
		{
			ladder[i].SetOnGround(true);
			//ladder[i].SetDirection(Vector3D(0, -1, 0));

			if(counter == 0)
				ladder[i].UpdateObject(Vector3D(), elapsed);

			ladder[numberOfSteps - 1].InitLine(Vector3D(ladder[0].GetPosition().x + ladder[0].GetWidth() / 2, ladder[0].GetPosition().y, ladder[0].GetPosition().z),
						Vector3D(ladder[numberOfSteps - 1].GetPosition().x + ladder[numberOfSteps - 1].GetWidth() / 2, ladder[numberOfSteps - 1].GetPosition().y + 10, ladder[numberOfSteps - 1].GetPosition().z));
		}
	}
}

bool Ladder::CollisionWithLadder(BoundingBox &theBox)
{
	if(ladder[numberOfSteps - 1].GetBoundingLine().HitTest(theBox))
	{
		return true;
	}

	return false;
}

bool Ladder::CollisionWithTopStep(BoundingBox &theBox)
{
	if(ladder[0].GetBoundingBox(Object::FULLBODY).HitTest(theBox))
	{
		return true;
	}
	return false;
}